package com.eqdd.yiqidian.utils;

import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.bean.PushMsg;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.bean.rebuild.GroupListResultBean;
import com.eqdd.yiqidian.db.DBUtil;
import com.eqdd.yiqidian.gen.GreenDaoManager;
import com.eqdd.yiqidian.gen.GroupListResultBeanDao;
import com.eqdd.yiqidian.gen.PushMsgDao;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

/**
 * Created by lzh on 17-1-5.
 */

public class GreenDaoUtil {


    public static PushMsgDao getPushMsgDao() {
        return GreenDaoManager.getInstance().getSession().getPushMsgDao();
    }

    public static GroupListResultBean getGroupById(String id) {
        List<GroupListResultBean> groupBeanList = getGroupDao().queryBuilder()
                .where(GroupListResultBeanDao.Properties.Groupid.eq(id))
                .list();

        return groupBeanList.size() > 0 ? groupBeanList.get(0) : null;
    }

    public static GroupListResultBeanDao getGroupDao() {
        return GreenDaoManager.getInstance().getSession().getGroupListResultBeanDao();

    }

    public static int insertGroup(GroupListResultBean bean) {
        GroupListResultBean groupById = getGroupById(bean.getGroupid());
        if (groupById != null) {
            getGroupDao().delete(groupById);
        }
        return (int) getGroupDao().insert(bean);
    }

    public static List<GroupListResultBean> getGroupAll() {
        List<GroupListResultBean> groupBeanList = getGroupDao().loadAll();
        return groupBeanList;
    }

    public static void deleteAllGroup() {
        List<GroupListResultBean> groupBeens = getGroupDao().loadAll();
        if (groupBeens == null || groupBeens.size() == 0) {
            return;
        }
        getGroupDao().deleteAll();
    }

    public static User getUser() {
        return DBUtil.getUserByName(SPUtil.getAccount("username", "")).getValue();

    }

    public static void deleteAllUser() {
        DBUtil.deleteAll();
    }

    public static void insert(User user) {
        DBUtil.insertUser(user);
        SPUtil.saveAccount("username", user.getUname());
        AppConstent.user = user;
    }

    public static PushMsg getPushMsg(int code) {

        return getPushMsgDao().load((long) code);

    }

    public static int getNum(int code) {
        PushMsg pushMsg = getPushMsgDao().load((long) code);
        return pushMsg == null ? 0 : pushMsg.getNum();
    }

    public static void setNum(int code, int num) {
        getPushMsgDao().insertOrReplace(new PushMsg((long) code, num));
        RxBus.getDefault().send(code);
        if (num == 0) {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CODE_CLEAR)
                    .params("userGuid", getUser().getGuid())
                    .params("code", code)
                    .execute(new JsonCallBack<HttpResult>() {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                        }
                    });
        }
    }

    public static void setNumNotPost(int code, int num) {
        getPushMsgDao().insertOrReplace(new PushMsg((long) code, num));
        if (num == 0) {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CODE_CLEAR)
                    .params("userGuid", getUser().getGuid())
                    .params("code", code)
                    .execute(new JsonCallBack<HttpResult>() {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                        }
                    });
        }
    }

    public static void clearPushCode() {
        getPushMsgDao().deleteAll();
    }
}
